package com.tjbklx1.thread.chap02.practise06;

/**
 * 不可变类
 * 可变与不可变的相互转化
 * @author Administrator
 *
 */
public class ImmutablePerson {

	private final String name;
	private final String address;

	public ImmutablePerson(String name, String address) {
		super();
		this.name = name;
		this.address = address;
	}

	public String getName() {
		return name;
	}

	public String getAddress() {
		return address;
	}
	
	//不可变类 不需要做同步
	@Override
	public String toString() {
		return "[ name = " + name + ",address = " + address + "]";
	}

	public ImmutablePerson(MutablePerson person) {
		synchronized (person) {	//重要的地方在这里，需要同步，防止线程改写
			this.name = person.getName();
			this.address = person.getAddress();
		}
	}

	public MutablePerson getMutablePerson() {
		return new MutablePerson(this);
	}

}
